/**
 *
 */
package com.gome.axwl.aoc.web.controller.user;

import com.anxl.sso.client.api.AccountInfo;
import com.anxl.sso.client.api.Token;
import com.anxl.sso.client.support.AnxlSsoSupport;
import com.gome.axwl.aoc.common.constants.LoginConstant;
import com.gome.axwl.aoc.common.util.BeanJsonUtil;
import com.gome.axwl.aoc.entity.mq.ImPushMsgMQEntityV31;
import com.gome.axwl.aoc.entity.user.LoginUserFromAccountInfo;
import com.gome.axwl.aoc.facade.CarrierSettleFacade;
import com.gome.axwl.aoc.facade.UserFacade;
import com.gome.axwl.aoc.service.sso.ServerGomeSsoClientService;
import com.gome.axwl.aoc.web.controller.mq.producer.ImProducer;
import com.gome.commons.core.ErrorCode;
import com.gome.commons.core.ResultData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户类
 */
@Api(tags = "用户")
@RestController
@PropertySource("classpath:anxlSsoServerConfig.properties")
public class UserController {
    private final static Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserFacade userFacade;

    @Autowired
    private ImProducer imProducer;

    @Value("${appKey}")
    private String appKey;

    @Autowired
    private ServerGomeSsoClientService ssoClientService;
    @Autowired
    private CarrierSettleFacade carrierSettleFacade;

    @ApiOperation(value = "获取菜单", notes = "development by chengh")
    @RequestMapping(value = "/user/queryMenus", method = RequestMethod.POST)
    public ResultData queryMenu(HttpServletRequest request) {
        Token token = (Token) request.getSession().getAttribute(AnxlSsoSupport.SSO_CONTEXT);
        log.info("---------------------token信息为:{}",token);
        return userFacade.queryResourceByUserAndRole(appKey, token.getUid());
    }

    @ApiOperation(value = "获取登录用户信息", notes = "development by chengh")
    @RequestMapping(value = "/user/getLoginUser", method = RequestMethod.POST)
    public ResultData getLoginUser(HttpServletRequest request, HttpServletResponse response) {
        //  Token token = (Token) request.getSession().getAttribute(AnxlSsoSupport.SSO_CONTEXT);
        AccountInfo accountInfo = (AccountInfo)request.getSession().getAttribute(LoginConstant.LOGIN_USER);
        if(accountInfo == null) {
            Token token = (Token) request.getSession().getAttribute(AnxlSsoSupport.SSO_CONTEXT);
            accountInfo = ssoClientService.getUserByLocalUserId(token.getUid(), request, response);
        }
        //加上审批权限
        LoginUserFromAccountInfo loginUserFromAccountInfo=BeanJsonUtil.beanConvert(accountInfo,LoginUserFromAccountInfo.class);
        ResultData<String> resultData = carrierSettleFacade.getUsersApprovallevel(loginUserFromAccountInfo.getAccountNo());
        if (!StringUtils.equals(resultData.getCode(),resultData.getCode())){
            log.error("获取用户在运营平台的审批权限失败");
        }else {
            loginUserFromAccountInfo.setPrivilegeLevel(resultData.getData());
        }
        return new ResultData<>(ErrorCode.SUCCESS, ErrorCode.SUCCESS_MSG, loginUserFromAccountInfo);
    }

    @ApiOperation(value = "登出", notes = "development by chengh")
    @RequestMapping(value = "/api/logout", method = RequestMethod.POST)
    public ResultData logout(HttpServletRequest request) {
        if (request.getSession().getAttribute(LoginConstant.LOGIN_USER) != null) {
            request.getSession().removeAttribute(LoginConstant.LOGIN_USER);          //销毁session
        }
        return ResultData.newSuccessResultData();
    }

    @ApiOperation(value = "登出", notes = "development by chengh")
    @RequestMapping(value = "/api/test", method = RequestMethod.GET)
    public ResultData testImProducer() {
        ImPushMsgMQEntityV31 mqEntityV31 = new ImPushMsgMQEntityV31();
        log.info("====produce:" + mqEntityV31.getMessageId());
        imProducer.send("gomeAxwlAocDriverIntegrationTag", mqEntityV31);
        return ResultData.newSuccessResultData();
    }

}
